var http = require('./utils/http.js')
import './utils/login-intercept';

App({
    require: function ($uri) {
        return require($uri);
    },

    globalData:{
        isProd: false,
        loginPro: null,
        userInfo: {},
        token: '',
        systemConfig: {},
        redirectPages: [
            '/pages/sale/list/list',
            '/pages/customer/list/list',
            '/pages/product/list/list',
            '/pages/me/index',
        ]
    },

  onLaunch: function (options) {
    let that = this
    console.log('app onlaunch')
    
    wx.getSystemInfo({
      success: e => {
        that.globalData.StatusBar = e.statusBarHeight;
        let custom = wx.getMenuButtonBoundingClientRect();
        that.globalData.Custom = custom;  
        that.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
      }
    })

    http.request('systemConfig/readAll', {}, function(config) {
        that.globalData.systemConfig = config
    })

    this.checkLoginStatus();
  },

  async checkLoginStatus() {
    this.globalData.loginPro = new Promise((resolve, reject) => {    
      http.login('weixinUser/login', {}, function(user) {
        getApp().globalData.userInfo = user
        if (user.bindUserFlag && user.bindUserFlag == '0') {
            wx.redirectTo({
              url: '/pages/login/index',
            })
            reject('un login')
        } else {
            getApp().globalData.token = user.token
            resolve(user)
        }
      }, function(msg) {
        reject(msg)
      });
    });
  }
})